package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.dialog.EditRateDialog
import com.changpay.zywy.bean.ShopAdRateBean
import kotlinx.android.synthetic.main.activity_shop_rate.*
import kotlinx.android.synthetic.main.title_layout.*
import java.util.HashMap


class SetShopRateActivity : BaseActivity(), IView {


    private var produce_id = ""
    override fun attachLayoutRes(): Int {
        return R.layout.activity_shop_rate
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
        finish()
    }

    override fun showMsg(msg: String) {
        showToast(msg)
        finish()
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
        finish()
    }


    override fun initData() {
    }


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
            R.id.iv_cloud_pay -> showEditDialog(1)
            R.id.iv_credit_card -> showEditDialog(2)
            R.id.iv_bank_card -> showEditDialog(3)
            R.id.iv_fast_pay -> showEditDialog(4)
            R.id.iv_code_pay -> showEditDialog(5)
            R.id.tv_apply -> updateRate()
            R.id.tv_submit -> loadProduceData()
            R.id.tv_sn_code -> {
                Intent(this, SelectShopSnActivity::class.java).run {
                    startActivityForResult(this, 100)
                }
            }
        }
    }

    override fun initView() {
        tv_title.text = "修改我的商户费率"
        iv_back.setOnClickListener(onClickListener)
        iv_cloud_pay.setOnClickListener(onClickListener)
        iv_credit_card.setOnClickListener(onClickListener)
        iv_bank_card.setOnClickListener(onClickListener)
        iv_fast_pay.setOnClickListener(onClickListener)
        iv_code_pay.setOnClickListener(onClickListener)
        tv_submit.setOnClickListener(onClickListener)
        tv_apply.setOnClickListener(onClickListener)
        tv_sn_code.setOnClickListener(onClickListener)

    }

    override fun start() {

    }


    private fun loadProduceData() {
        val param = hashMapOf<String, String>()
        param["key"] = token
        param["sn_code"] = tv_sn_code.text.toString()
        param["client"] = "android"
        RetrofitHelper.service.adjustmentRate(param).sss(this)
        {
            setData(it.result)
        }
    }

    private var canChange: Boolean = false  // 用于控制是否可以被编辑


    private lateinit var cloud_merchant_rate: String

    private lateinit var bankCard_merchant_rate: String

    private lateinit var lineCard_merchant_rate: String

    private lateinit var quickPay_merchant_rate: String

    private lateinit var scaveCode_merchant_rate: String

    private fun setData(dateBean: ShopAdRateBean?) {
        canChange = if (dateBean != null) {
            tv_cloud_pay.text = dateBean.cloud_merchant_rate + "%"
            tv_credit_card.text = dateBean.lineCard_merchant_rate + "%"
            tv_bank_card.text = dateBean.bankCard_merchant_rate + "%"
            tv_fast_pay.text = dateBean.quickPay_merchant_rate + "%"
            tv_code_pay.text = dateBean.scaveCode_merchant_rate + "%"
            true
        } else {
            tv_cloud_pay.text = "0.00%"
            tv_credit_card.text = "0.00%"
            tv_bank_card.text = "0.00%"
            tv_fast_pay.text = "0.00%"
            tv_code_pay.text = "0.00%"
            false
        }
        tv_cloud_pay_change.text = ""
        tv_credit_card_change.text = ""
        tv_bank_card_change.text = ""
        tv_fast_pay_change.text = ""
        tv_code_pay_change.text = ""
        cloud_merchant_rate = ""
        bankCard_merchant_rate = ""
        lineCard_merchant_rate = ""
        quickPay_merchant_rate = ""
        scaveCode_merchant_rate = ""
        tv_apply.isEnabled = false
    }

    private fun showEditDialog(type: Int) {
        if (canChange) {
            var title = ""
            var dialogClickListener: EditRateDialog.OnDialogClickListener? = null
            when (type) {
                1 -> {
                    title = "云闪付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { _, inputText ->
                        tv_cloud_pay_change.text = "修改为  $inputText%"
                        cloud_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                2 -> {
                    title = "信用卡"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_credit_card_change.text = "修改为  $inputText%"
                        lineCard_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                3 -> {
                    title = "银行卡"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_bank_card_change.text = "修改为  $inputText%"
                        bankCard_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                4 -> {
                    title = "快捷支付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_fast_pay_change.text = "修改为  $inputText%"
                        quickPay_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
                5 -> {
                    title = "扫码支付"
                    dialogClickListener = EditRateDialog.OnDialogClickListener { v, inputText ->
                        tv_code_pay_change.text = "修改为  $inputText%"
                        scaveCode_merchant_rate = inputText
                        tv_apply.isEnabled = true
                    }
                }
            }
            val builder = EditRateDialog.Builder(this)
            builder.setInputText("输入的值保留小数点后三位").setTitleText(title).setOnDialogClickListener(dialogClickListener)
            val build = builder.build<EditRateDialog>()
            build.show(supportFragmentManager)
        }
    }


    private fun updateRate() {
        val params = HashMap<String, String>()
        params["key"] = token
        params["sn_code"] = tv_sn_code.text.toString()
        if (et_sn_phone.text.toString().isNotEmpty()) {
            params["mobile"] = et_sn_phone.text.toString()
        }
        if (cloud_merchant_rate.isNotEmpty()) {
            params["cloud_merchant_rate"] = cloud_merchant_rate
        }

        if (bankCard_merchant_rate.isNotEmpty()) {
            params["bankCard_merchant_rate"] = bankCard_merchant_rate
        }

        if (lineCard_merchant_rate.isNotEmpty()) {
            params["lineCard_merchant_rate"] = lineCard_merchant_rate
        }

        if (quickPay_merchant_rate.isNotEmpty()) {
            params["quickPay_merchant_rate"] = quickPay_merchant_rate
        }

        if (scaveCode_merchant_rate.isNotEmpty()) {
            params["scaveCode_merchant_rate"] = scaveCode_merchant_rate
        }

        params["client"] = "android"
        params["gc_id"] = intent.getStringExtra("gc_id")
        RetrofitHelper.service.updateSubmitAdjustmentRate(params).sss(this)
        {
            showToast(it.result.toString())
            finish()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 300) {
            tv_sn_code.text = data?.getStringExtra("sn_number")
            loadProduceData()
        }
    }

}
